package com.hljx.job;

import com.hljx.entity.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.batch.MyBatisCursorItemReader;
import org.mybatis.spring.batch.builder.MyBatisCursorItemReaderBuilder;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * @BelongsProject: hljx
 * @BelongsPackage: com.hljx.job
 * @Author: lp
 * @CreateTime: 2023-10-23  08:57
 * @Description: TODO
 * @Version: 1.0
 */
@Slf4j
@Configuration
public class Read {

    @StepScope
    @Bean(name = "adviceItemReader")
    public MyBatisCursorItemReader<AdviceItem> itemReaderMybatis(@Qualifier("HisSqlSessionFactory") SqlSessionFactory readSqlSessionFactory, @Value("#{@datesParameters}") Map<String, Object> datesParameters) {
        log.info("itemReader~~~~~~~~~~~~~");
        return new MyBatisCursorItemReaderBuilder<AdviceItem>()
                .sqlSessionFactory(readSqlSessionFactory)
                .parameterValues(datesParameters)
                .queryId("com.hljx.mapper.his.HisAdviceItemMapper.listAll")
                .build();
    }

    @StepScope
    @Bean(name = "ifFreeItemItemReader")
    public MyBatisCursorItemReader<IfFeeitemlist> ifFreeItemMybatis(@Qualifier("HisSqlSessionFactory") SqlSessionFactory readSqlSessionFactory,
                                                                    @Value("#{@datesParameters}") Map<String, Object> datesParameters) {
        return new MyBatisCursorItemReaderBuilder<IfFeeitemlist>()
                .sqlSessionFactory(readSqlSessionFactory)
                .parameterValues(datesParameters)
                .queryId("com.hljx.mapper.his.HisItemMapper.listIfFeeitemAll")
                .build();
    }

    @StepScope
    @Bean(name = "ipMomDodReader")
    public MyBatisCursorItemReader<Ipmomdod> ipMomDodMybatis(@Qualifier("HisSqlSessionFactory") SqlSessionFactory readSqlSessionFactory,
                                                             @Value("#{@datesParameters}") Map<String, Object> datesParameters) {
        return new MyBatisCursorItemReaderBuilder<Ipmomdod>()
                .sqlSessionFactory(readSqlSessionFactory)
                .parameterValues(datesParameters)
                .queryId("com.hljx.mapper.his.HisItemMapper.listIpmomdodAll")
                .build();
    }

    @StepScope
    @Bean(name = "ipMonMexReader")
    public MyBatisCursorItemReader<Ipmonmex> ipMonMexMybatis(@Qualifier("HisSqlSessionFactory") SqlSessionFactory readSqlSessionFactory,
                                                             @Value("#{@datesParameters}") Map<String, Object> datesParameters) {
        return new MyBatisCursorItemReaderBuilder<Ipmonmex>()
                .sqlSessionFactory(readSqlSessionFactory)
                .parameterValues(datesParameters)
                .queryId("com.hljx.mapper.his.HisItemMapper.listIpMonMexAll")
                .build();
    }


    @StepScope
    @Bean(name = "unDrugDetailReader")
    public MyBatisCursorItemReader<UndrugDetail> UnDrugDetailReader(@Qualifier("HisSqlSessionFactory") SqlSessionFactory readSqlSessionFactory, @Value("#{@datesParameters}") Map<String, Object> datesParameters) {
        log.info("itemReader~~~~~~~~~~~~~");
        return new MyBatisCursorItemReaderBuilder<UndrugDetail>()
                .sqlSessionFactory(readSqlSessionFactory)
                .parameterValues(datesParameters)
                .saveState(false)
                .queryId("com.hljx.mapper.his.HisItemMapper.listUnDrugDetailAll")
                .build();
    }

    @StepScope
    @Bean(name = "clinicInvoiceReader")
    public MyBatisCursorItemReader<ClinicInvoiceInfo> clinicInvoiceReader(@Qualifier("HisSqlSessionFactory") SqlSessionFactory readSqlSessionFactory, @Value("#{@datesParameters}") Map<String, Object> datesParameters) {
        log.info("itemReader~~~~~~~~~~~~~");
        return new MyBatisCursorItemReaderBuilder<ClinicInvoiceInfo>()
                .sqlSessionFactory(readSqlSessionFactory)
                .parameterValues(datesParameters)
                .queryId("com.hljx.mapper.his.HisItemMapper.listClinicInvoice")
                .build();
    }

    @StepScope
    @Bean(name = "iiBedUseRecordReader")
    public MyBatisCursorItemReader<IiBeduserecord> iiBedUseRecordReader(@Qualifier("HisSqlSessionFactory") SqlSessionFactory readSqlSessionFactory, @Value("#{@datesParameters}") Map<String, Object> datesParameters) {
        log.info("itemReader~~~~~~~~~~~~~");
        return new MyBatisCursorItemReaderBuilder<IiBeduserecord>()
                .sqlSessionFactory(readSqlSessionFactory)
                .parameterValues(datesParameters)
                .queryId("com.hljx.mapper.his.HisItemMapper.listIiBedUseRecord")
                .build();
    }

    @StepScope
    @Bean(name = "iiShiftDataReader")
    public MyBatisCursorItemReader<IiShiftdata> iiShiftDataReader(@Qualifier("HisSqlSessionFactory") SqlSessionFactory readSqlSessionFactory, @Value("#{@datesParameters}") Map<String, Object> datesParameters) {
        log.info("itemReader~~~~~~~~~~~~~");
        return new MyBatisCursorItemReaderBuilder<IiShiftdata>()
                .sqlSessionFactory(readSqlSessionFactory)
                .parameterValues(datesParameters)
                .queryId("com.hljx.mapper.his.HisItemMapper.listIiShiftData")
                .build();
    }

    @StepScope
    @Bean(name = "incomeExpensesReader")
    public MyBatisCursorItemReader<IncomeExpenses> incomeExpensesReader(@Qualifier("HisSqlSessionFactory") SqlSessionFactory readSqlSessionFactory, @Value("#{@datesParameters}") Map<String, Object> datesParameters) {
        log.info("itemReader~~~~~~~~~~~~~");
        return new MyBatisCursorItemReaderBuilder<IncomeExpenses>()
                .sqlSessionFactory(readSqlSessionFactory)
                .parameterValues(datesParameters)
                .queryId("com.hljx.mapper.his.HisItemMapper.listIncomeExpenses")
                .build();
    }


    @StepScope
    @Bean(name = "llOneBackExpensesReader")
    public MyBatisCursorItemReader<LlOneBackExpenses> llOneBackExpensesReader(@Qualifier("HisSqlSessionFactory") SqlSessionFactory readSqlSessionFactory, @Value("#{@datesParameters}") Map<String, Object> datesParameters) {
        log.info("itemReader~~~~~~~~~~~~~");
        return new MyBatisCursorItemReaderBuilder<LlOneBackExpenses>()
                .sqlSessionFactory(readSqlSessionFactory)
                .parameterValues(datesParameters)
                .queryId("com.hljx.mapper.his.HisItemMapper.listLlOneBackExpenses")
                .build();
    }

    @StepScope
    @Bean(name = "mrmsbaseReader")
    public MyBatisCursorItemReader<Mrmsbase> mrmsbaseReader(@Qualifier("HisSqlSessionFactory") SqlSessionFactory readSqlSessionFactory, @Value("#{@datesParameters}") Map<String, Object> datesParameters) {
        log.info("itemReader~~~~~~~~~~~~~");
        return new MyBatisCursorItemReaderBuilder<Mrmsbase>()
                .sqlSessionFactory(readSqlSessionFactory)
                .parameterValues(datesParameters)
                .queryId("com.hljx.mapper.his.HisItemMapper.listMrmsbase")
                .build();
    }

    @StepScope
    @Bean(name = "mrmsdymrReader")
    public MyBatisCursorItemReader<Mrmsdymr> mrmsdymrReader(@Qualifier("HisSqlSessionFactory") SqlSessionFactory readSqlSessionFactory, @Value("#{@datesParameters}") Map<String, Object> datesParameters) {
        log.info("itemReader~~~~~~~~~~~~~");
        return new MyBatisCursorItemReaderBuilder<Mrmsdymr>()
                .sqlSessionFactory(readSqlSessionFactory)
                .parameterValues(datesParameters)
                .queryId("com.hljx.mapper.his.HisItemMapper.listMrmsdymr")
                .build();
    }


    @StepScope
    @Bean(name = "opApplyReader")
    public MyBatisCursorItemReader<OpApply> opApplyReader(@Qualifier("HisSqlSessionFactory") SqlSessionFactory readSqlSessionFactory, @Value("#{@datesParameters}") Map<String, Object> datesParameters) {
        log.info("itemReader~~~~~~~~~~~~~");
        return new MyBatisCursorItemReaderBuilder<OpApply>()
                .sqlSessionFactory(readSqlSessionFactory)
                .parameterValues(datesParameters)
                .queryId("com.hljx.mapper.his.HisItemMapper.listOpApply")
                .build();
    }

    @StepScope
    @Bean(name = "ordSeedoctrecordReader")
    public MyBatisCursorItemReader<OrdSeedoctrecord> ordSeedoctrecordReader(@Qualifier("HisSqlSessionFactory") SqlSessionFactory readSqlSessionFactory, @Value("#{@datesParameters}") Map<String, Object> datesParameters) {
        log.info("itemReader~~~~~~~~~~~~~");
        return new MyBatisCursorItemReaderBuilder<OrdSeedoctrecord>()
                .sqlSessionFactory(readSqlSessionFactory)
                .parameterValues(datesParameters)
                .queryId("com.hljx.mapper.his.HisItemMapper.listOrdSeedoctrecord")
                .build();
    }

    @StepScope
    @Bean(name = "rMzClassCollectYardReader")
    public MyBatisCursorItemReader<RMzClassCollectYard> rMzClassCollectYardReader(@Qualifier("HisSqlSessionFactory") SqlSessionFactory readSqlSessionFactory, @Value("#{@datesParameters}") Map<String, Object> datesParameters) {
        log.info("itemReader~~~~~~~~~~~~~");
        return new MyBatisCursorItemReaderBuilder<RMzClassCollectYard>()
                .sqlSessionFactory(readSqlSessionFactory)
                .parameterValues(datesParameters)
                .queryId("com.hljx.mapper.his.HisItemMapper.listRMzClassCollectYard")
                .build();
    }

    @StepScope
    @Bean(name = "rStatMzClassCollectReader")
    public MyBatisCursorItemReader<RStatMzClassCollect> rStatMzClassCollectReader(@Qualifier("HisSqlSessionFactory") SqlSessionFactory readSqlSessionFactory, @Value("#{@datesParameters}") Map<String, Object> datesParameters) {
        log.info("itemReader~~~~~~~~~~~~~");
        return new MyBatisCursorItemReaderBuilder<RStatMzClassCollect>()
                .sqlSessionFactory(readSqlSessionFactory)
                .parameterValues(datesParameters)
                .queryId("com.hljx.mapper.his.HisItemMapper.listRStatMzClassCollect")
                .build();
    }

    @StepScope
    @Bean(name = "rStatMzCollectReader")
    public MyBatisCursorItemReader<RStatMzCollect> rStatMzCollectReader(@Qualifier("HisSqlSessionFactory") SqlSessionFactory readSqlSessionFactory, @Value("#{@datesParameters}") Map<String, Object> datesParameters) {
        log.info("itemReader~~~~~~~~~~~~~");
        return new MyBatisCursorItemReaderBuilder<RStatMzCollect>()
                .sqlSessionFactory(readSqlSessionFactory)
                .parameterValues(datesParameters)
                .queryId("com.hljx.mapper.his.HisItemMapper.listRStatMzCollect")
                .build();
    }

    @StepScope
    @Bean(name = "iaFeelistRead")
    public MyBatisCursorItemReader<IaFeelist> iaFeelistRead(@Qualifier("HisSqlSessionFactory") SqlSessionFactory readSqlSessionFactory) {
        log.info("itemReader~~~~~~~~~~~~~");
        return new MyBatisCursorItemReaderBuilder<IaFeelist>()
                .sqlSessionFactory(readSqlSessionFactory)
                .queryId("com.hljx.mapper.his.HisAdviceItemMapper.listAllIaFeelist")
                .build();
    }

    @StepScope
    @Bean(name = "mrmsbasetimeRead")
    public MyBatisCursorItemReader<Mrmsbasetime> mrmsbasetimeRead(@Qualifier("HisSqlSessionFactory") SqlSessionFactory readSqlSessionFactory) {
        log.info("itemReader~~~~~~~~~~~~~");
        return new MyBatisCursorItemReaderBuilder<Mrmsbasetime>()
                .sqlSessionFactory(readSqlSessionFactory)
                .queryId("com.hljx.mapper.his.HisAdviceItemMapper.listAllMrmsbasetime")
                .build();
    }

    @StepScope
    @Bean(name = "privDeptRead")
    public MyBatisCursorItemReader<PrivDept> privDeptRead(@Qualifier("HisSqlSessionFactory") SqlSessionFactory readSqlSessionFactory) {
        log.info("itemReader~~~~~~~~~~~~~");
        return new MyBatisCursorItemReaderBuilder<PrivDept>()
                .sqlSessionFactory(readSqlSessionFactory)
                .queryId("com.hljx.mapper.his.HisAdviceItemMapper.listAllPrivDept")
                .build();
    }

    @StepScope
    @Bean(name = "rDepartmentRead")
    public MyBatisCursorItemReader<RDepartment> rDepartmentRead(@Qualifier("HisSqlSessionFactory") SqlSessionFactory readSqlSessionFactory) {
        log.info("itemReader~~~~~~~~~~~~~");
        return new MyBatisCursorItemReaderBuilder<RDepartment>()
                .sqlSessionFactory(readSqlSessionFactory)
                .queryId("com.hljx.mapper.his.HisAdviceItemMapper.listAllRDepartment")
                .build();
    }

    @StepScope
    @Bean(name = "rEmployeeRead")
    public MyBatisCursorItemReader<REmployee> rEmployeeRead(@Qualifier("HisSqlSessionFactory") SqlSessionFactory readSqlSessionFactory) {
        log.info("itemReader~~~~~~~~~~~~~");
        return new MyBatisCursorItemReaderBuilder<REmployee>()
                .sqlSessionFactory(readSqlSessionFactory)
                .queryId("com.hljx.mapper.his.HisAdviceItemMapper.listAllREmployee")
                .build();
    }

    @StepScope
    @Bean(name = "VmHdTreatRead")
    public MyBatisCursorItemReader<VmHdTreat> VmHdTreatRead(@Qualifier("XtSqlSessionFactory") SqlSessionFactory readSqlSessionFactory, @Value("#{@datesParameters}") Map<String, Object> datesParameters) {
        log.info("itemReader~~~~~~~~~~~~~");
        return new MyBatisCursorItemReaderBuilder<VmHdTreat>()
                .sqlSessionFactory(readSqlSessionFactory)
                .parameterValues(datesParameters)
                .queryId("com.hljx.mapper.xt.XTItemMapper.listVmHdTreat")
                .build();
    }

    @StepScope
    @Bean(name = "VmHdTreatXgRead")
    public MyBatisCursorItemReader<VmHdTreatXg> VmHdTreatXgRead(@Qualifier("xtXgSqlSessionFactory") SqlSessionFactory readSqlSessionFactory, @Value("#{@datesParameters}") Map<String, Object> datesParameters) {
        log.info("itemReader~~~~~~~~~~~~~");
        return new MyBatisCursorItemReaderBuilder<VmHdTreatXg>()
                .sqlSessionFactory(readSqlSessionFactory)
                .parameterValues(datesParameters)
                .queryId("com.hljx.mapper.xtxg.XTXGItemMapper.listVmHdTreat")
                .build();
    }


    @StepScope
    @Bean(name = "MedOperationMasterRead")
    public MyBatisCursorItemReader<MedOperationMaster> MedOperationMasterRead(@Qualifier("SMSqlSessionFactory") SqlSessionFactory readSqlSessionFactory, @Value("#{@datesParameters}") Map<String, Object> datesParameters) {
        log.info("itemReader~~~~~~~~~~~~~");
        return new MyBatisCursorItemReaderBuilder<MedOperationMaster>()
                .sqlSessionFactory(readSqlSessionFactory)
                .parameterValues(datesParameters)
                .queryId("com.hljx.mapper.sm.SMItemMapper.listMedOperationMaster")
                .build();
    }

    @StepScope
    @Bean(name = "CpoeProjectDischargeExeReader")
    public MyBatisCursorItemReader<CpoeProjectDischargeExe> CpoeProjectDischargeExeReader(@Qualifier("HisSqlSessionFactory") SqlSessionFactory readSqlSessionFactory, @Value("#{@datesParameters}") Map<String, Object> datesParameters) {
        log.info("itemReader~~~~~~~~~~~~~");
        return new MyBatisCursorItemReaderBuilder<CpoeProjectDischargeExe>()
                .sqlSessionFactory(readSqlSessionFactory)
                .parameterValues(datesParameters)
                .queryId("com.hljx.mapper.his.HisItemMapper.listCpoeProjectDischargeExe")
                .build();
    }

    @StepScope
    @Bean(name = "opRecordReader")
    public MyBatisCursorItemReader<OpRecord> opRecordReader(@Qualifier("HisSqlSessionFactory") SqlSessionFactory readSqlSessionFactory, @Value("#{@datesParameters}") Map<String, Object> datesParameters) {
        log.info("itemReader~~~~~~~~~~~~~");
        return new MyBatisCursorItemReaderBuilder<OpRecord>()
                .sqlSessionFactory(readSqlSessionFactory)
                .parameterValues(datesParameters)
                .queryId("com.hljx.mapper.his.HisItemMapper.listOpRecord")
                .build();
    }

    @StepScope
    @Bean(name = "rRegisterReader")
    public MyBatisCursorItemReader<RRegister> rRegisterReader(@Qualifier("HisSqlSessionFactory") SqlSessionFactory readSqlSessionFactory, @Value("#{@datesParameters}") Map<String, Object> datesParameters) {
        log.info("itemReader~~~~~~~~~~~~~");
        return new MyBatisCursorItemReaderBuilder<RRegister>()
                .sqlSessionFactory(readSqlSessionFactory)
                .parameterValues(datesParameters)
                .queryId("com.hljx.mapper.his.HisItemMapper.listRRegister")
                .build();
    }

    @StepScope
    @Bean(name = "vHljxFeedetailReader")
    public MyBatisCursorItemReader<VHljxFeedetail> vHljxFeedetailReader(@Qualifier("JxSqlSessionFactory") SqlSessionFactory readSqlSessionFactory, @Value("#{@datesParameters}") Map<String, Object> datesParameters) {
        log.info("itemReader~~~~~~~~~~~~~");
        return new MyBatisCursorItemReaderBuilder<VHljxFeedetail>()
                .sqlSessionFactory(readSqlSessionFactory)
                .parameterValues(datesParameters)
                .queryId("com.hljx.mapper.jx.JXItemMapper.listVHljxFeedetail")
                .build();
    }

    @StepScope
    @Bean(name = "vCssdWorkloadReader")
    public MyBatisCursorItemReader<vCssdWorkload> vCssdWorkloadMybatis(@Qualifier("GYSqlSessionFactory") SqlSessionFactory readSqlSessionFactory,
                                                                    @Value("#{@datesParameters}") Map<String, Object> datesParameters){
        return new MyBatisCursorItemReaderBuilder<vCssdWorkload>()
                .sqlSessionFactory(readSqlSessionFactory)
                .parameterValues(datesParameters)
                .queryId("com.hljx.mapper.gy.GYItemMapper.listVCssdWorkloadAll")
                .build();
    }


    @StepScope
    @Bean
    public Map<String, Object> datesParameters(
            @Value("#{jobParameters['year']}") String year,
            @Value("#{jobParameters['month']}") String month,
            @Value("#{jobParameters['startDate']}") String startDate,
            @Value("#{jobParameters['endDate']}") String endDate,
            @Value("#{jobParameters['start']}") String start,
            @Value("#{jobParameters['end']}") String end,
            @Value("#{jobParameters['startTime']}") String startTime,
            @Value("#{jobParameters['endTime']}") String endTime,
            @Value("#{jobParameters['splitTableName']}") String splitTableName) {
        Map<String, Object> map = new HashMap<>();
        map.put("year", year);
        map.put("times", month);
        map.put("startDate", startDate);
        map.put("endDate", endDate);
        map.put("start", start);
        map.put("end", end);
        map.put("startTime", startTime);
        map.put("endTime", endTime);
        map.put("splitTableName", splitTableName);
        return map;
    }
}
